
import java.util.logging.Logger;

class MainThread extends Thread
{
    Logger logger = MyLogger.getLogger();
    GameFrame1 gameFrame;

    public int getSliceGap() {
        return sliceGap;
    }

    public void setSliceGap(int sliceGap) {
        this.sliceGap = sliceGap;
    }

    //时间片最小切换时间
    private int sliceGap;
    TcpServerThread tcpServerThread;

    TcpClientThread tcpClientThread;

    public MainThread()
    {
        gameFrame = new GameFrame1();

        if (Global.thisDeviceType == SelfType.DEVICE_TYPE_SERVER)
        {
            tcpServerThread = new TcpServerThread();
            tcpServerThread.start();
            gameFrame.setTcpServer(tcpServerThread.tcpServer);
            setSliceGap(10);
        }
        else if (Global.thisDeviceType == SelfType.DEVICE_TYPE_CLIENT)
        {
            tcpClientThread = new TcpClientThread();
            tcpClientThread.start();
            gameFrame.setTcpClient(tcpClientThread.tcpClient);
            setSliceGap(2);
        }
    }

    public void run()
    {
        logger.info("run");
        Boolean runFlag = true;
        while (runFlag)
        {
            if (Global.thisDeviceType == SelfType.DEVICE_TYPE_SERVER)
            {
                gameFrame.RunServer();
            }
            else if (Global.thisDeviceType == SelfType.DEVICE_TYPE_CLIENT)
            {
                gameFrame.RunClient();
            }

            /**/
            try
            {
                Thread.sleep(getSliceGap());
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
                throw new RuntimeException(e);
            }

        }
    }
}
